<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <!-- 引入layui 样式-->
    <link rel="stylesheet" href="../../static/layer/css/layui.css"/>
    <link rel="stylesheet" href="../../static/css/font-awesome.min.css">
    <script type="text/javascript" src="../../static/layer/layui.js"></script>
    <style>
        .grid-color {
            margin: 25px 20px 20px 20px;
            padding: 10px;
            line-height: 40px;
            text-align: center;
            background-color: #4494F0;
            color: #fff;
        }

        .div-text {
            font-size: 18px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div>
    <div class="layui-row layui-col-space1">
        <!--租户模块-->
        <div class="loan_item"></div>
        <!--<div class="layui-col-md3">
            <div class="div-text grid-color">
                <div>借款人：11</div>
                <div>借款金额：900000</div>
                <div>到期时间：2018-3-3</div>
            </div>
        </div>-->
    </div>
</div>
<script type="text/javascript" src="../../static/js/common/queryUtils.js"></script>
<script type="text/javascript" src="../../static/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../../static/js/common/authUtils.js"></script>
<script>
    // 加载元素控件
    var table;
    layui.use(['jquery', 'element', 'layer', 'laydate', 'form', 'table'], function () {
        $ = layui.jquery, element = layui.element, layer = layui.layer, form = layui.form, table = layui.table, laydate = layui.laydate;
        var loan_price = QueryUtils.GetQueryString("loan_price");
        //初始化时间
        laydate.render({
            elem: '#START_DATE'
            , format: 'yyyy-MM-dd'
        });
        laydate.render({
            elem: '#END_DATE'
            , format: 'yyyy-MM-dd'
        });

    });
    //初始化
    var init ={
        //默认加载
        initFirst:function(){
            //加载首页贷款 首页租户列表和数据
            $.ajax({
                url: "/LoanController/findloaninfo",
                data: {},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.errcode === "0") {
                        if (!!res) {
                            init.initData(res);
                        }
                    } else if (res.errcode === "1") {
                        layer.msg(res.errmsg, function () {
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });
        },

        //加载合同信息
        initData:function (res) {
            if (res.data.length > 0) {
                var loan_number = 0;
                var total_price = 0;//计算总金额
                for (let k of res.data){
                    $(".loan_item").append(`<div class="layui-col-md3 loan_children">
                                                <input type="hidden" class="Loan_id" value="${k.ID}" >
                                                <div class="div-text grid-color">
                                                    <div>借款方：${k.COMPANY_NAME}</div>
                                                    <div>借款金额：${init.fixed(k.LOAN_PRICE)}</div>
                                                    <div>到期时间：${k.CON_END_DATE}</div>
                                                </div>
                                            </div>`);
                    total_price += k.LOAN_PRICE;
                    ++loan_number;
                }
                parent.$(".loan_number").html(loan_number);
                parent.$(".total_price").html(init.fixed(total_price));
                //查看合同
                $(".loan_children").click(function () {
                   var ID=$(this).children().val()&&$(this).children().val();
                    parent.layer.open({
                        zIndex: 1000,
                        type: 2,    //弹出类型
                        title: '查看合同',   //标题
                        closeBtn: 1,    //显示关闭按钮
                        shade: [0.3],   //遮罩透明度
                        area: ['80%', '90%'],  //打开窗口的宽高
                        time: 0,    //几秒后自动关闭，默认0时不关闭
                        anim: 1,    //动画效果
                        resize: false,  //设置成不允许拉伸
                        content: '../../../webhtml/contract/loan_info.html?CONTRACT_ID=' + ID + ''  //iframe的url
                    });
                });
            }
        },

        //总收入
        totalRevenue:function () {
            $.ajax({
                url: "/LoanController/totalRevenue",
                data: {},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.errcode === "0") {
                        console.log(res.data[0])
                        res.data[0]&& parent.$(".zsy").html(init.fixed(res.data[0].TOTAL_PRICE));
                    } else if (res.errcode === "1") {
                        layer.msg(res.errmsg, function () {
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });
        },

        //保留2位小数
        fixed:function (str) {
            if (!str) str = '0.00';
            let ret = Math.round(parseFloat(str) * 100) / 100;
            let decimal = ret.toString().split('.');
            if (decimal.length === 1) {
                return ret.toString() + '.00'
            }
            ;
            if (decimal.length > 1) {
                if (decimal[1].length < 2) {
                    return ret.toString() + '0'
                }
                return ret
            }
            ;
            return ret;
        }
    }
    init.initFirst();
    init.totalRevenue();
</script>
</body>
</html>
